﻿/* Icon 扩展类
 */

/*待测试*/
namespace LSLib.Drawing
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Runtime.InteropServices;

    public class IconEx
    {
        /// <summary>获取图标
        /// </summary>
        /// <param name="file">文件(夹)路径</param>
        /// <returns>图标资源</returns>
        public static Icon GetIcon(string file)
        {
            API.Drawing.SHFILEINFO shfi = new API.Drawing.SHFILEINFO();

            API.Drawing.SHGetFileInfo(
                file,
                (uint)FileAttributes.Normal,
                ref shfi,
                (uint)Marshal.SizeOf(shfi),
                (uint)(API.Drawing.SHGFI.Icon | API.Drawing.SHGFI.TypeName)); //取得Icon和TypeName

            return (Icon)Icon.FromHandle(shfi.hIcon).Clone();
        }

        /// <summary>销毁图标
        /// </summary>
        /// <param name="hIcon">待销毁图标的句柄</param>
        public static void DestroyIcon(IntPtr hIcon)
        {
            API.Drawing.DestroyIcon(hIcon);
        }
    }
}